// ve = vue element
let ve_provider;
$(function () {
    let default_provider_info = {id: -1, name: "", city: "", description: "", location: "", phoneNumber: "", businesses: {}};
    let default_business_info = {id: -1, name: "", summary: ""};
    ve_provider = new Vue({
        el: "#provider-data-box",
        data: {
            page_index: 1,
            page_num: 1,
            providers: [default_provider_info],
            businesses: [default_business_info],
            pointed_business_id: -1,
            pointed_city: "",
            go: 1
        },
        methods: {
            get_paging_data(page) {
                ve_provider.get_pages_amount();
                if (page < 1)
                    page = 1;
                else if (page > ve_provider.page_num)
                    page = ve_provider.page_num;

                ajax_get("/provider/page/" + (page - 1), {
                    business_id: ve_provider.pointed_business_id,
                    city: ve_provider.pointed_city
                }, function (response) {
                    if (response.code == null) {
                        ve_provider.providers = response;
                    }
                }, "Getting paging data of providers")
            },
            get_pages_amount() {
                ajax_get("/provider/page", {
                    business_id: ve_provider.pointed_business_id,
                    city: ve_provider.pointed_city
                }, function (response) {
                    ve_provider.page_num = response;
                }, "Getting pages amount of providers")
            },
            render_modal_box(id) {
                ve_provider_info.reset_checked_business();

                ve_provider_info.message = "";
                ve_provider_info.errorMessage = "";
                ve_provider_info.update_button_show = true;
                if (id < 0) {
                    Object.assign(ve_provider_info.provider, default_provider_info);
                    ve_provider_info.updating = false;
                } else {
                    Object.assign(ve_provider_info.provider, ve_provider.providers.find(provider => provider.id === id));
                    ve_provider_info.updating = true;
                }
                ve_provider_info.check_proving_business();
            },
            reset_search() {
                ve_provider.pointed_city = "";
                ve_provider.pointed_business_id = -1;
                ve_provider.get_paging_data(0);
            }
        }
    });


    let ve_provider_info = new Vue({
        el: "#provider-info",
        data: {
            provider: {id: -1, name: "", city: "", description: "", location: "", phoneNumber: "", businesses: {}},
            businesses: [default_business_info],
            errorMessage: "",
            message: "",
            updating: true,
            update_button_show: false
        },
        methods: {
            create() {
                if (ve_provider_info.updating) {
                    ve_provider_info.update();
                    return;
                }
                ve_provider_info.updating = false;
                let provider = ve_provider_info.provider;
                let selected_services = ve_provider_info.get_selected_service_list();
                ajax_post("/provider", {
                    name: provider.name,
                    description: provider.description,
                    location: provider.location,
                    city: provider.city,
                    phoneNumber: provider.phoneNumber,
                    businesses: selected_services
                }, function (response) {
                    if (response.code == null) {
                        ve_provider_info.message = "创建成功";
                        ve_provider_info.close_info_modal();
                    }
                }, "Creating provider")
            },
            update() {
                ve_provider_info.updating = true;
                let provider = ve_provider_info.provider;
                let selected_services = ve_provider_info.get_selected_service_list();
                ajax_put("/provider", {
                    id : provider.id,
                    name: provider.name,
                    description: provider.description,
                    location: provider.location,
                    city: provider.city,
                    phoneNumber: provider.phoneNumber,
                    businesses: selected_services
                }, function (response) {
                    if (response.code == null) {
                        ve_provider_info.message = "更新成功";
                        ve_provider_info.close_info_modal();
                    }
                }, "Updating provider")
            },
            deleteIt() {
                if (confirm("确认要删除 " + ve_provider_info.provider.name +" ?")) {
                    ajax_delete("/provider/" + ve_provider_info.provider.id, function (response) {
                        if (response.code == null) {
                            ve_provider_info.message = "已删除";
                            ve_provider_info.close_info_modal();
                        }
                    }, "Deleting provider")
                }
            },
            close_info_modal() {
                ve_provider_info.update_button_show = false;
                setTimeout(function () {
                    ve_provider.get_paging_data(0);
                    $("#provider-info").modal("hide");
                }, 1000);
            },
            contains_business(id) {
                let businesses = ve_provider.businesses;
                for (let business of businesses) {
                    if (business.id === id)
                        return true;
                }
                return false;
            },
            //
            check_proving_business() {
                for (let providing_business of ve_provider_info.provider.businesses) {
                    for (let business of ve_provider_info.businesses) {
                        if (business.id === providing_business.id) {
                            business.check = 1;
                        }
                    }
                }
            },
            reset_checked_business() {
                for (let business of ve_provider_info.businesses) {
                    business.check = -1;
                }
            },
            check_business(id, event) {
                for (let business of ve_provider_info.businesses) {
                    if (business.id === id) {
                        let button = event.currentTarget;
                        if (business.check === 1) {
                            business.check = -1;
                            $(button).removeClass("bg-success");
                            $(button).addClass("bg-secondary");
                        } else {
                            business.check = 1;
                            $(button).removeClass("bg-secondary");
                            $(button).addClass("bg-success");
                        }
                        return;
                    }
                }
            },
            business_button_class(business_id) {
                for (let business of this.businesses) {
                    if (business.id === business_id) {
                        return (business.check != null && business.check === 1) ? "bg-success" : "bg-secondary";
                    }
                }
            },
            get_selected_service_list() {
                let list = [];
                for (let business of ve_provider_info.businesses) {
                    if (business.check === 1)
                        list.push({ id: business.id })
                }
                return list;
            }
        },
        computed: {
            fields_empty() {
                let provider = this.provider;
                return provider.name === "" || provider.description === "" || provider.location === "" || provider.city === "" ||
                    provider.phoneNumber.length !== 11 || provider.businesses.length <= 0;
            },
            showing_img() {
                return this.provider.image === "" ? "https://www.easyicon.net/api/resizeApi.php?id=1228554&size=128" : this.provider.image;
            },
            showing_name() {
                return this.provider.name === "" ? "新服务提供商" : this.provider.name;
            },
            update_or_create_method() {
                return this.updating ? "update" : "create";
            },
            update_or_create() {
                return this.updating ? "更新" : "创建";
            },
            provided_business(business_id) {
                for (let business of this.businesses) {
                    if (business.id === business_id && business.check != null && business.check === 1)
                        return true;
                }
                return false;
            }
        }
    });

    let ve_business = new Vue({
        el: "#business-data-box",
        data: {
            businesses: [default_business_info]
        },
        methods: {
            get_business() {
                get_business_for_all();
            },
            render_modal_box(id) {
                ve_business_info.message = "";
                ve_business_info.errorMessage = "";
                ve_business_info.update_button_show = true;
                if (id < 0) {
                    Object.assign(ve_business_info.business, default_business_info);
                    ve_business_info.updating = false;
                } else {
                    Object.assign(ve_business_info.business, ve_business.businesses.find(business => business.id === id));
                    ve_business_info.updating = true;
                }
            }
        }
    });

    let ve_business_info = new Vue({
        el: "#business-info",
        data: {
            business: {id: -1, name: "", summary: ""},
            updating: true,
            update_button_show: false,
            message: "",
            errorMessage: ""
        },
        methods: {
            create() {
                if (ve_business_info.updating) {
                    ve_business_info.update();
                    return;
                }
                let business = ve_business_info.business;
                ajax_post("/business", {
                    name: business.name,
                    summary: business.summary
                }, function (response) {
                    if (response.code == null) {
                        ve_business_info.message = "创建成功";
                        ve_business_info.close_info_modal(false);
                    }
                }, "Creating business")
            },
            update() {
                let business = ve_business_info.business;
                ajax_put("/business", {
                    id : business.id,
                    summary: business.summary
                }, function (response) {
                    if (response.code == null) {
                        ve_business_info.message = "更新成功";
                        ve_business_info.close_info_modal(false);
                    }
                }, "Updating business")
            },
            deleteIt() {
                if (confirm("确认要删除 " + ve_business_info.business.name +" ? ")) {
                    ajax_delete("/business/" + ve_business_info.business.id, function (response) {
                        if (response.code == null) {
                            ve_business_info.message = "已删除";
                            ve_business_info.close_info_modal(true);
                        }
                    }, "Deleting business")
                }
            },
            close_info_modal(is_delete) {
                ve_business_info.update_button_show = false;
                setTimeout(function () {
                    if (is_delete)
                        ve_provider.get_paging_data(0);
                    get_business_for_all();
                    $("#business-info").modal("hide");
                }, 1000);
            }
        },
        computed: {
            fields_empty() {
                return this.business.name === "" || this.business.summary === "";
            },
            showing_name() {
                return this.business.name === "" ? "新增服务" : this.business.name;
            },
            update_or_create_method() {
                return this.updating ? "update" : "create";
            },
            update_or_create() {
                return this.updating ? "更新" : "创建";
            }
        }
    });

    function get_business_for_all() {
        ajax_get("/business", {}, function (response) {
            if (response.code == null) {
                ve_provider.businesses = response;
                ve_provider_info.businesses = response;
                ve_business.businesses = response;
            } else
                console.log(response.message)
        })
    }

    ve_provider.get_paging_data(0);
    get_business_for_all();
});